﻿using UnityEngine;
using System.Collections;
using System.Net;
using System.IO;
using LitJson;
using UnityEngine.UI;
using System.Linq;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using LuaFramework;
namespace Game
{
    public class SDKController : MonoBehaviour
    {
        private static SDKController _controller;
        public static SDKController instance
        {
            get
            {
                if (_controller == null)
                {
                    GameObject go = new GameObject("SDKController");
                    _controller = go.AddComponent<SDKController>();
                }
                return _controller;
            }
        }
        private void Awake()
        {
            DontDestroyOnLoad(this);
        }
        /// <summary>
        ///微信登录按钮事件
        /// </summary>
        public void ClickWXLoginBtn()
        {
#if UNITY_ANDROID
        Debug.Log("OnClickWeiXinLogin+++++++++++++");
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.Call("OnClickWXLogin");
#elif UNITY_IPHONE
        OnClickWXLoginios();
#endif
        }
        void OnWXloginCallBack(string code)
        {
            string url = AppConst.WXAppLoginCallBackUrl;
            JsonData data = new JsonData();
            data["code"] = code;
            string content = HttpPostData(url, "data=" + data.ToJson());
            JsonData jd = JsonMapper.ToObject(content);
            if (jd["status"].ToJson() == "-1")
            {
                Debug.Log("登录失败");
            }
            Util.CallMethod("SDKController", "OnWXLoginCallBack", jd["data"].ToJson());
            //com.guojin.mj.net.message.login.Login Log = com.guojin.mj.net.message.login.Login.create("WEIXIN", null, jd["data"].ToJson(), longitude, latitude);
            //SocketMgr.GetInstance().Send(com.guojin.mj.net.Net.instance.write(Log));         
        }
        public string HttpPostData(string url, string param)
        {
            var result = string.Empty;
            //注意提交的编码 这边是需要改变的 这边默认的是Default：系统当前编码
            byte[] postData = System.Text.Encoding.UTF8.GetBytes(param);
            // 设置提交的相关参数 
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
            request.Method = "POST";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.ContentLength = postData.Length;
            // 提交请求数据 
            Stream outputStream = request.GetRequestStream();
            outputStream.Write(postData, 0, postData.Length);
            outputStream.Close();
            HttpWebResponse response;
            Stream responseStream;
            StreamReader reader;
            string srcString;
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("UTF-8"));
            srcString = reader.ReadToEnd();
            result = srcString;   //返回值赋值 通过返回值判断是否登录成功
            reader.Close();
            return result;
        }
    }
}
